package com.guli.ware.service.impl;

import com.guli.common.utils.R;
import com.guli.ware.feign.MemberFeignService;
import com.guli.ware.vo.MemberAddressFareVo;
import com.guli.ware.vo.MemberAddressVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.ware.dao.WareInfoDao;
import com.guli.ware.entity.WareInfoEntity;
import com.guli.ware.service.WareInfoService;
import org.springframework.util.StringUtils;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {

    @Autowired
    MemberFeignService memberFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<WareInfoEntity> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(key)) {
            wrapper.eq("id", key).or().eq("areacode", key).or().like("name", key);
        }
        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public R getFare(Long addrId) {
        //根据地址ID获取地址信息
        MemberAddressVo address = memberFeignService.getAddressFare(addrId);
        String phone = address.getPhone();
        BigDecimal bigDecimal = new BigDecimal(phone.substring(phone.length() - 1));
        MemberAddressFareVo memberAddressFareVo = new MemberAddressFareVo();
        memberAddressFareVo.setFare(bigDecimal);
        memberAddressFareVo.setMemberAddressVo(address);
        return R.ok().put("data", memberAddressFareVo);
    }

}